#include<stdio.h>
#include<stdlib.h>
#include<string.h> 
#include<time.h> 
// 商品结构体 
typedef struct
{
	int number;
	char name[20];
	float inprice;
	float outprice;
	int count;
}product;

// 存放数据的链表 
struct data{
	product p;
	struct data * next;
};

/*
函数声明 
*/
void menu(int* choose);											//功能菜单 
struct data* load(struct data* head);					//加载数据 
void addProduct(struct data** end);						//添加商品 
void delProduct(struct data* head);						//删除商品 
void write(struct data* head);							//写入数据 
void purchase(struct data* head);						//商品进货 
void settlement(struct data* head, struct data* res);	//商品结算
//void getTime(FILE *fp);
void writelist(struct data* res);						//写入清单 
/*
主函数 
*/ 
int main(){
	// 模式 
	int choose;
	// 销售清单 
	struct data* res = (struct data*) malloc(sizeof(struct data));
	res -> next = NULL;
	
	struct data* head = (struct data* )malloc(sizeof(struct data));
	struct data* end = NULL;
	head -> next = NULL;
	while(1){
		menu(&choose);
		end = load(head);
		if(choose == 1){
			purchase(head);
		}else if(choose == 2){
			settlement(head, res);
			writelist(res); 
		}else if(choose == 3){
			addProduct(&end);
		}else if(choose == 4){
			delProduct(head);
		}
	}
	
	return 0;
}

/*
功能菜单:
	进货：1
	结算：2
	添加：3
	删除：4 
*/
void menu(int* choose){
	// 清屏 
	system("cls");
	// 交互界面 
	printf("******************************************************\n");
	printf("*                   商店销售管理系统                 *\n"); 
	printf("******************************************************\n");
	printf("*                      输入 1 进货                   *\n");
	printf("*                      输入 2 结算                   *\n");
	printf("*                  输入 3 添加新的商品               *\n");
	printf("*                输入 4 删除不需要的商品             *\n");
	printf("******************************************************\n");
	// 输入模式 
	scanf("%d", choose);
	return; 
}
/*
读取数据 
*/
struct data* load(struct data *head){
	FILE* fp;
	if((fp = fopen("data.txt","r")) == NULL){
		printf("加载存档时发生错误\n");
		exit(1);
	}
	// 临时存放文件数据 
	int number_t;
	char name_t[20];
	float inprice_t;
	float outprice_t;
	int count_t;
	struct data *q = head;
	// 读取 
	while(!feof(fp)){
		fscanf(fp, "%d", &number_t);
		fscanf(fp, "%s", name_t);
		fscanf(fp, "%f", &inprice_t);
		fscanf(fp, "%f", &outprice_t);
		fscanf(fp, "%d", &count_t);
		struct data *tmp = (struct data* )malloc(sizeof(struct data));
		tmp -> next = NULL;
		// 存入链表 
		tmp -> p.number = number_t;
		strcpy(tmp -> p.name, name_t);
		tmp -> p.inprice = inprice_t;
		tmp -> p.outprice = outprice_t;
		tmp -> p.count = count_t;
		q -> next = tmp;
		q = q -> next;
		//printf("%d %s %f %f %d", number_t, name_t, inprice_t, outprice_t, count_t);
		
	}
	fclose(fp);
	return q; 
} 
/*
清单写入磁盘 
*/
void writelist(struct data* res){
	FILE* fp;
	fp = fopen("list.txt","a");
	res = res -> next;
	while(res != NULL){
		fprintf(fp, "\n%d %s %f %f %d \n", res -> p.number, res -> p.name, res -> p.inprice, res -> p.outprice, res -> p.count);
		//fprintf(fp, "%s\n", res -> p.name);
		//fprintf(fp, "%f\n", res -> p.inprice);
		//fprintf(fp, "%f\n", res -> p.outprice);
		//fprintf(fp, "%d\n", res -> p.count);
		res = res -> next;
	}
	
	fclose(fp);
	printf("数据写入成功\n");
	return;
}
/*
数据写入磁盘 
*/
void write(struct data* head){
	system("del data.txt");
	FILE* fp;
	fp = fopen("data.txt","w+");
	head = head -> next;
	while(head != NULL){
		fprintf(fp, "\n%d\n", head -> p.number);
		fprintf(fp, "%s\n", head -> p.name);
		fprintf(fp, "%f\n", head -> p.inprice);
		fprintf(fp, "%f\n", head -> p.outprice);
		fprintf(fp, "%d", head -> p.count);
		head = head -> next;
	}
	
	fclose(fp);
	printf("数据写入成功\n");
	return;
}

/*
进货：1 
*/
void purchase(struct data* head){
	printf("提示：输入-1完成进货\n");
	int n, cnt;
	while(1){
		printf("输入商品的编号：\n");
		scanf("%d", &n);
		if(n == -1) break;
		printf("输入进货数量：\n"); 
		scanf("%d", &cnt) ;
		
		struct data* q = head;
		q = q -> next;
		while(q != NULL){
			if(q -> p.number == n){
				q -> p.count += cnt;
				break;
			}
			q = q -> next;
		}
	}
	write(head);
	printf("进货完成\n");
	getchar();
	getchar();
	
}

/*
结算 
*/
void settlement(struct data* head, struct data* res){
	// 总售价 
	double money_all = 0;
	printf("提示：输入-1进行结算\n");
	int n,cnt;
	
	struct data* tmp;
	// 工作指针 
	tmp = res;
	while(1){
		printf("输入商品编号：\n");
		scanf("%d", &n);
		// 结算
		if(n == -1){
			struct data* tmp2 = res;
			tmp2 = tmp2 -> next;
			int i = 1;
			while(tmp2 != NULL){
				printf("项目%d 编号：%d 商品名称：%s 售价：%f 数量：%d\n", i, tmp2->p.number, tmp2->p.name, tmp2->p.outprice, tmp2->p.count);
				money_all += (tmp2 -> p.outprice) * (tmp2 -> p.count);
				i++;
				tmp2 = tmp2 -> next;
			}
			printf("总计：%lf元\n",money_all);
			break;
		} 
		printf("输入购买数量：\n"); 
		scanf("%d", &cnt);
		
		// 寻找商品信息 
		struct data* q = head;
		q = q -> next;
		while(q != NULL){
			if(q -> p.number == n){
				q -> p.count -= cnt;
				struct data* newp = (struct data*) malloc(sizeof(struct data));
				newp -> next = NULL;
				newp -> p.number = q -> p.number;
				strcpy(newp -> p.name, q -> p.name);
				newp -> p.inprice = q -> p.inprice;
				newp -> p.outprice = q -> p.outprice;
				// 此时count代表购买数量 
				newp -> p.count = cnt;
				tmp -> next = newp;
				tmp = tmp -> next;
				break;
			}
			q = q -> next;
		}
	}
	write(head);
	//return res; 
	getchar();
	getchar();
	return;
	
}


/*
添加新的商品: 3 
*/
void addProduct(struct data** end){
	struct data *newp = (struct data* )malloc(sizeof(struct data));
	newp -> next =  NULL;
	printf("输入新商品的编号：\n");
	scanf("%d", &newp -> p.number);
	printf("输入新商品的名字：\n");
	scanf("%s", newp -> p.name);
	printf("输入新商品的进价：\n");
	scanf("%f", &newp -> p.inprice);
	printf("输入新商品的售价：\n");
	scanf("%f", &newp -> p.outprice);
	printf("输入新商品的库存数量：\n");
	scanf("%d", &newp -> p.count);
	// 加入链表 
	(*end) -> next = newp;
	(*end) = (*end) -> next;
	// 写入文件
	FILE* fp;
	fp = fopen("data.txt","a");
	fprintf(fp, "\n%d\n", newp -> p.number);
	fprintf(fp, "%s\n", newp -> p.name);
	fprintf(fp, "%f\n", newp -> p.inprice);
	fprintf(fp, "%f\n", newp -> p.outprice);
	fprintf(fp, "%d", newp -> p.count);
	
	printf("成功添加\n");
	fclose(fp);
	getchar();
	getchar();
	return;
}

/*
删除不需要的商品：4 
*/
void delProduct(struct data* head){
	int n;
	printf("输入要删除的商品的编号：\n");
	scanf("%d", &n);
	int flag = 0; // 标记是否存在 
	struct data* tmp = head, *head_t = head;
	head = head -> next;
	while(head != NULL){
		// 找到商品 
		if(head -> p.number == n){
			tmp -> next = head -> next;
			flag = 1;
			break;
		}
		tmp = head;
		head = head -> next;
	}
	if(flag){
		write(head_t);
		printf("删除成功\n"); 
		head_t = head_t -> next;
		while(head_t != NULL){
			printf("%d\n", head_t -> p.number);
			printf("%s\n", head_t -> p.name);
			printf("%f\n", head_t -> p.inprice);
			printf("%f\n", head_t -> p.outprice);
			printf("%d\n", head_t -> p.count);
			head_t = head_t -> next;
		}
	}else{
		printf("该商品不存在\n");
	}
	getchar();
	return;
} 
/*
void getTime(FILE *fp){
	//获取当前系统时间
	time_t tTime;//距离1900年1月1日的秒数
	time(&tTime);
	fprintf(fp, "%lld", tTime);
}
*/